package com.cynteka.cyfoman.ui.collection;

import com.cynteka.cyfoman.core.ComplexPropertyResolver;
import com.cynteka.cyfoman.core.IPropertyResolver;
import com.cynteka.cyfoman.core.collection.CollectionIdProvider;
import com.cynteka.cyfoman.core.collection.CollectionRegistry;
import com.cynteka.cyfoman.core.collection.ICollection;
import com.cynteka.cyfoman.core.config.ColumnMD;
import com.cynteka.cyfoman.ui.Reloadable;
import com.cynteka.cyfoman.ui.TableValueProvider;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * User: Sergey
 * Date: 03.08.11
 * Time: 10:48
 */
public class CollectionTableModel extends AbstractTableModel implements TableModel, Reloadable, TableValueProvider, CollectionIdProvider {

    private List<ColumnMD> columns;
    private String collectionId;
    private CollectionRegistry collectionRegistry;
    private List items;
    private IPropertyResolver propertyResolver = new ComplexPropertyResolver();

    public IPropertyResolver getPropertyResolver() {
        return propertyResolver;
    }

    public void setPropertyResolver(IPropertyResolver propertyResolver) {
        this.propertyResolver = propertyResolver;
    }

    public CollectionRegistry getCollectionRegistry() {
        return collectionRegistry;
    }

    public void setCollectionRegistry(CollectionRegistry collectionRegistry) {
        this.collectionRegistry = collectionRegistry;
    }

    public String getCollectionId() {
        return collectionId;
    }

    public void setCollectionId(String collectionId) {
        this.collectionId = collectionId;
    }

    public List<ColumnMD> getColumns() {
        return columns;
    }

    public void setColumns(List<ColumnMD> columns) {
        this.columns = columns;
    }

    protected void initItems() {
        if (items == null) {
            ICollection collection = collectionRegistry.getCollection(collectionId);
            Collection colItems = collection.getItems(null);
            items = colItems == null ? new ArrayList() : new ArrayList(colItems);
        }
    }

    @Override
    public int getRowCount() {
        initItems();
        return items.size();
    }

    @Override
    public int getColumnCount() {
        return columns.size();
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columns.get(columnIndex).getTitle();
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return columns.get(columnIndex).getClass();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        initItems();
        ColumnMD column = columns.get(columnIndex);
        return getValue(rowIndex, column.getName());
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void reload() {
        items = null;
        initItems();
        fireTableDataChanged();
    }

    public Object getValue(int rowIndex, String columnName) {
        Object o = items.get(rowIndex);
        Object value = propertyResolver.getProperty(o, columnName);
        return value;
    }

    public Object getValue(int rowIndex) {
        Object o = items.get(rowIndex);
        return o;
    }
}
